package com.jsj.controller;

import com.jsj.pojo.Admin;
import com.jsj.pojo.Repair;
import com.jsj.service.RepairService;
import com.jsj.service.impl.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Controller
public class AdminController {

    @Autowired
    AdminServiceImpl adminService;
    @Autowired
    RepairService repairService;
//登录
    @RequestMapping("/tologin")
    @ResponseBody
    public Admin login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestParam("duty") String duty,
                        Model model, HttpSession session) {
        Admin admin = adminService.queryAdmin(username, password, duty);//登录
        System.out.println(admin);
        if (!StringUtils.isEmpty(admin)) {
            session.setAttribute("loginUser", username);
            int adminId = admin.getId();
            session.setAttribute("adminId",adminId);
            System.out.println("admin--------:"+admin.toString());
            return admin;
        } else {
            model.addAttribute("msg", "用户名或密码错误或权限错误");
            session.setAttribute("msg", "用户名或密码错误或权限错误");
            return null;
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){

        session.removeAttribute("loginUser");
        session.invalidate();//清除session对象中的所有信息
        System.out.println("---退出---");

        return "login";
    }



    @RequestMapping("/queryAllAdmin")
    @ResponseBody
    public List<Admin> queryAllAdmin(){
        List<Admin> admins = adminService.queryAllAdmin();
        System.out.println("---queryAllAdmin---"+admins);
        return admins;
    }

    @RequestMapping("/addAdmin")
    @ResponseBody
    public Boolean addAdmin(Admin admin){
        System.out.println("--addAdmin--:"+admin);
        Boolean flag=null;
        if(admin.getImg()==null||"".equals(admin.getImg())){
            String imageToBase64 = ImageToBase64("target/classes/static/base64.jpg");
            System.out.println(imageToBase64);
            admin.setImg(imageToBase64);
            //admin.setImg("");

             flag = adminService.addAdmin(admin);
            return true;
        }else {
            flag = adminService.addAdmin(admin);
            return flag;
        }
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Boolean deleteByid(int id){
        Boolean flag = adminService.deleteById(id);
        return flag;
    }

    @Value("${file.uploadFolder}")
    private String uploadDir;
    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "/updateByAdminId",method = RequestMethod.POST)
    @ResponseBody
    public Boolean updateByAdminId(String base64Data, Admin admin,HttpServletRequest request) throws IOException {

        /*File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdir();
        }*/
        if (admin.getId()==1){
            admin.setDuty("超级管理员");
        }

        String dataPrix = ""; //base64格式前头
        String data = "";//实体部分数据
        if(base64Data==null||"".equals(base64Data)){
            //request.setAttribute("msg","上传失败，上传图片数据为空");
            //return false;
           //String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
            String imageToBase64 = ImageToBase64("target/classes/static/base64.jpg");
            System.out.println(imageToBase64);
            admin.setImg(imageToBase64);
            //admin.setImg("");

            Boolean flag = adminService.updateByAdminId(admin);
            return true;
        }else {
            admin.setImg(base64Data);
            Boolean flag = adminService.updateByAdminId(admin);

            return true;
        }


    }
//必须要有超级管理员权限
    @RequestMapping("/queryCheckStatus")
    @ResponseBody
    public List<Repair> queryCheckStatus(String status){
        System.out.println("--超级管理员的请求类型---status----"+status);
        List<Repair> repairList = repairService.queryCheckStatus(status);
        return repairList;

    }


//审核通过/拒绝
    @RequestMapping("/checkRepair")
    @ResponseBody
    public Boolean checkRepair(int repairId,String check){
        Boolean flag = repairService.checkRepair(repairId,check);

        return flag;
    }

    private static String ImageToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过的字节数组字符串
        String base64=encoder.encode(Objects.requireNonNull(data));
        System.out.println("本地图片转换Base64:" + base64);
        return base64;
    }

    @RequestMapping("/queryAdminById")
    @ResponseBody
    public Admin queryAdminById(int id){
        Admin admin = adminService.queryAdminById(id);
        return admin;
    }


}
